﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Input;
using System.Diagnostics;

namespace ShredTheLog.Converters
{
    [ValueConversion(typeof(KeyGesture), typeof(string))]
    public sealed class KeyGestureValueConverter : ConverterBase, IValueConverter
    {
        private static readonly KeyGestureConverter KeyGestureConverter = new KeyGestureConverter();

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var v = (KeyGesture)value;
            if (v != null)
                return KeyGestureConverter.ConvertToInvariantString(v);
            else
                return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var v = (string)value;
            if (v != null)
            {
                try
                {
                    return KeyGestureConverter.ConvertFromInvariantString(v);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(String.Format("Failed to convert string \"{0}\" into KeyGesture.", v));
                    Trace.WriteLine(ex.ToString());
                    return null;
                }
            }
            else
                return null;
        }

        public static readonly KeyGestureValueConverter Default = new KeyGestureValueConverter();
    }
}
